var HOST = "http://192.168.1.11:3300";

class Request {
    static instance = null;

    commonParams = {
        channel: 'platform',
        appid: JSBridge.getMetaDataValue('AHAO_APPID'),
        gameid: JSBridge.getMetaDataValue('AHAO_GAMEID')
    };

    static getInstance() {
        if (this.instance == null) {
            this.instance = new Request();
        }
        return this.instance;
    }

    // 封装方法用于拼接公共参数
    appendCommonParams(data) {
        var token = JSBridge.getToken()
        if (token) {
            data.token = token
        }
        return { ...data, ...this.commonParams };
    }

    post(url, data, callback) {
        $.ajax({
            type: "POST",
            url: HOST + url,
            data: this.appendCommonParams(data), // 使用拼接公共参数的方法
            success: function (response) {
                if (response.code == 0) {
                    callback(response);
                } else {
                    JSBridge.showToast(response.msg);
                    console.error(new Error(response.msg));
                }
            },
            error: function (error) {
                console.error("Error:", error);
                JSBridge.showToast(error.message);
            }
        });
    }

    get(url, data, callback) {
        $.ajax({
            type: "GET",
            url: HOST + url,
            data: this.appendCommonParams(data), // 使用拼接公共参数的方法
            success: function (response) {
                if (response.code == 0) {
                    callback(response);
                } else {
                    JSBridge.showToast(response.msg);
                    console.error(new Error(response.msg));
                }
            },
            error: function (error) {
                console.error("Error:", error);
                JSBridge.showToast("网络错误");
            }
        });
    }
}


var request = Request